package demo.ClassLoad;


import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * @Auther: hjy(lc))
 * @Date: 2022/8/5-08-05-9:25
 * @Description：demo.ClassLoad     反射
 */
/*
    1、基本类型
        Student s = new Student(1） 左边是类的模板加变量     右边是具体的对象    一般使用
        Class c = int.class;     通过反射获取

    2、引用类型
        类类型
        Class<Student> c1 = Student.class;

        数组类型
        Student[] arr = new Student[5];
        Class<Student[]> c2 = Student[].class;

        接口类型
        class<Action> c3 = Action.class
 */
public class reflect {
    public static void main(String[] args) throws ClassNotFoundException {
        //第一种方式： 通过类.class
        Class c1 = int.class;
        //第二种方式：通过对象.getClass()    [引用类型]
        Student stu = new Student();
        Class<? extends Student> c2 = stu.getClass();
        Action a = new Action() {
        };
        Class<? extends Action> c3 = a.getClass();
        //第三种方式：Class.forName()     常用！！！！！！加载并初始化
        Class<?> aClass = Class.forName("demo.ClassLoad.Temp");
    }
}
@Role(name = "admin")
class Student{
    //定义属性/字段
    public String name;   //姓名
    private int  sno;      //学号
    public final int age = 10;       //年龄
    final String test = "hello";
    //Student student = new Student("hjyHello",1);

    public Student(String name, int sno ) {
        this.name = name;
        this.sno = sno;

    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", sno=" + sno +
                ", age=" + age +
                '}';
    }
    private Student(String name){
        this.name = name;
    }
    public Student() {
    }

    //定义方法
    //修饰符 返回值 方法名字([参数]){}
    @Role(name = "guest")
    public void sayHello(){
        System.out.println("hello,name = " + name);
    }

//    public static void main(String[] args) {
//        //使用该模板和对象
//        demo.classuse.Student s = new demo.classuse.Student();
//        //设置属性    对象.属性
//        s.name = "hjy";
//        s.sno = 12;
//        s.age = 18;
//        //调用方法
//        s.sayHello();
//        String str1 = "hello";
//        String str2 = new String("hello");
//        System.out.println(str1 == str2);
//        test(str2);
//        System.out.println(str2);
//    }
    @Role(name = "customer")
    public static void test(String s){
        s = "world";
        System.out.println(s);
    }
}
interface Action{}

@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@interface Role{
    String name();
}
